# -*- coding:utf-8 -*-
import qutils, time
import socket, traceback, os, sys, struct, qwaterdb

class HTCPDevice(object):
    HTCP_DEV_OFFLINE    = -1
    HTCP_DEV_LIVE       = 0
    HTCP_DEV_START      = 1
    HTCP_DEV_STOP       = 2
    HTCP_DEV_USING_ME   = 3
    HTCP_DEV_USING_OH   = 4
    
    def __init__(self, aSocket, aDid, aStatus):
        print '------ init :', aDid
        self.did    = aDid              #设备ID
        self.value  = 0                 #当前水表字数
        self.status = aStatus           #0-live, 1-start, 2-stop
        self.sock   = aSocket           #socket
        self.slive  = qutils.timeUnix() #心跳开始时间
        self.elive  = self.slive        #最后一次心跳时间
        self.using  = ''                #当前正在使用的卡号
        self.stime  = 0                 #延迟开始时间
        self.etime  = 0                 #延迟关闭时间

        #add by tommy
        self.updateDeviceInfo()
    
    def updateDeviceInfo(self):
        qutils.rctLogger.info('------ updateDeviceInfo : %s' % self.did)
        if qutils.isEmpty(self.did) :
            return

        tmpRet = qwaterdb.dbSelectDevInfo(self.did)
        qutils.rctLogger.info("dbSelectDevInfo ret: %s", str(tmpRet))
        if tmpRet == qwaterdb.K_RCT_MSG_ERROR or not tmpRet or len(tmpRet) < 3 :
            return

        if not qutils.isEmpty(tmpRet[0]) :
            self.stime  = int(tmpRet[0])
        if not qutils.isEmpty(tmpRet[1]) :
            self.etime  = int(tmpRet[1])
        if not qutils.isEmpty(tmpRet[2]) and tmpRet[2] != "idle":
            self.using  = tmpRet[2]

    def isLive(self):
        if qutils.timeUnix() - self.elive > 70 :
            return self.HTCP_DEV_OFFLINE
        return self.HTCP_DEV_LIVE

    def isDevStatus(self):
        return self.status

    def setDevStatus(self, aSock, aSta):
        self.sock   = aSock 
        self.elive  = qutils.timeUnix()

        if self.status == aSta :
            return

        self.status = aSta
        if not qutils.isEmpty(self.using) :
            if self.status == self.HTCP_DEV_START :
                for i in range(0, 5):
                    tmpRet = qwaterdb.dbSelectStart(self.using, self.did, self.value)
                    if int(tmpRet) == qwaterdb.K_RCT_MSG_OK :
                        print "db-start ..... ok:",tmpRet
                        break
                    print "db-start ..... fail:",tmpRet
                    time.sleep(1)
            if self.status == self.HTCP_DEV_STOP :
                for i in range(0, 5):
                    tmpRet = qwaterdb.dbSelectStop(self.using, self.did, self.value)
                    if int(tmpRet) == qwaterdb.K_RCT_MSG_OK  :
                        print "db-stop ..... ok:",tmpRet
                        break
                    print "db-stop ..... fail:",tmpRet
                    time.sleep(1)
                self.using = ''
        
    def setDevValue(self, aSock, aVal):
        print '------ setDevValue :',self.did, aVal
        self.value  = aVal
        self.sock   = aSock 
        self.elive  = qutils.timeUnix()
        if qutils.isEmpty(self.using) :
            return 0
        if self.status == self.HTCP_DEV_START :
            tmpRet = qwaterdb.dbSelectDevUpdate(self.using, self.did, aVal)
            if int(tmpRet) == qwaterdb.K_RCT_MSG_OK :
                print "recieve stop device command from server"
                return 1
            if int(tmpRet) == qwaterdb.K_RCT_MSG_UPDATE :
                self.updateDeviceInfo()
                return 1
        return 0

    def isDevStop(self, aUsing):
        qutils.rctLogger.info('------ isDevStop :%s,%s,%s' % (self.did, self.using, aUsing))
        if self.status != self.HTCP_DEV_START :
            qutils.rctLogger.error("HTCP_DEV_LIVE")
            return self.HTCP_DEV_LIVE

        if qutils.isEmpty(self.using) or qutils.isEmpty(aUsing) :
            qutils.rctLogger.error("using or aUsing is null")
            return self.HTCP_DEV_LIVE

        if self.using == aUsing :
            qutils.rctLogger.info("HTCP_DEV_STOP")
            return self.HTCP_DEV_STOP

        tmpRet = qwaterdb.dbSelectBelongMCard(self.using, aUsing)
        if int(tmpRet) == qwaterdb.K_RCT_MSG_OK :
            qutils.rctLogger.info("HTCP_DEV_STOP")
            return self.HTCP_DEV_STOP

        qutils.rctLogger.info("setDevStop end")
        return self.isLive()

    def isDevStart(self, aUsing):
        qutils.rctLogger.info('------ isDevStart :%s,%s,%s' % (self.did, self.using, aUsing))
        if self.isLive() == self.HTCP_DEV_OFFLINE :
            qutils.rctLogger.error("HTCP_DEV_OFFLINE")
            return self.HTCP_DEV_OFFLINE

        if self.status == self.HTCP_DEV_STOP or qutils.isEmpty(self.using):
            qutils.rctLogger.info("HTCP_DEV_START")
            if qwaterdb.dbIsOpenDevice('1','1','1') == qwaterdb.K_RCT_MSG_OK: 
                qutils.rctLogger.info("qwaterdb.K_RCT_MSG_OK")
                self.using = aUsing
                return self.HTCP_DEV_START
            else:
                qutils.rctLogger.info("qwaterdb.K_RCT_MSG_ERROR")

        if self.using == aUsing :
            qutils.rctLogger.info("HTCP_DEV_USING_ME")
            return self.HTCP_DEV_USING_ME

        print '------ setDevStart end'
        return self.HTCP_DEV_USING_OH
